package com.birdy.common.action;

import com.birdy.common.service.ICardService;
import com.birdy.dto.Response;
import com.birdy.vo.Card;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author birdy
 * @date 2022/4/1 10:37 PM
 */
@Slf4j
@RestController
@RequestMapping("/card/")
@CrossOrigin
public class CardAction {
    @Autowired
    private ICardService cardService;
    @GetMapping("list")
    public Response getCardList() {
        log.info("a user come in");
        try {
            List<Card> cardList = cardService.getCardList();
            return Response.builder().res(Response.SUCCESS).msg("success")
                    .data("list", cardList).build();
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.commonResponseByRes(false, "服务器正在维护");
        }
    }
    @GetMapping("send")
    public Object sendMessage() {
        try {
            DefaultMQProducer mqProducer = new DefaultMQProducer("FacekillGroup");
            mqProducer.setNamesrvAddr("rocketmq-server:9876");
            mqProducer.start();
            Message msg = new Message("TopicCommon", "hello"
                    .getBytes(RemotingHelper.DEFAULT_CHARSET)); // 消息需要二进制
            SendResult send = mqProducer.send(msg);
            mqProducer.shutdown();
            return send;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
